<template>
  <div class="meeting-container">
    <div class="company-info">
      <img alt="公司logo" class="company-logo" src="https://qushen-outsourcing.oss-cn-beijing.aliyuncs.com/hongyou/logo.png" />
      <span class="company-name">红有软件股份有限公司</span>
    </div>
    <div class="meeting-header">
      <h1 class="meeting-title">{{ meetingName }}</h1>
      <div class="attendance-info">
        <div class="info-item">
          <span class="label">应到人数</span>
          <span class="value">{{ totalExpected }}</span>
        </div>
        <div class="info-item">
          <span class="label">实到人数</span>
          <span class="value">{{ totalPresent }}</span>
        </div>
      </div>
    </div>

    <el-row class="h-full meeting-content" :gutter="60">
      <el-col class="flex items-center" :span="12">
        <face />
      </el-col>

      <el-col class="flex items-center" :span="12">
        <div class="time-container">
          <div class="digital-time">{{ currentTime }}</div>
          <div class="date-info">
            {{ currentDate }}
            <span class="week">{{ weekDay }}</span>
          </div>
          <div class="qr-section">
            <el-image :src="qrCodeUrl" style="width: 180px; height: 180px" />
            <p class="qr-tip">欢迎参加会议</p>
            <p class="qr-desc">
              除了刷脸签到，您还可以通过
              <br />
              会议详情→扫码签到扫描以上二维码
              <br />
              进行签到
            </p>
          </div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import dayjs from 'dayjs'
import { ElMessage } from 'element-plus'
import QRCode from 'qrcode'
import { onBeforeUnmount, onMounted, ref } from 'vue'
import { useRoute } from 'vue-router'
import Public from '~/src/api/public'
import face from './components/face.vue'

const route = useRoute()
const currentDate = ref('')
const weekDay = ref('')
const currentTime = ref('')
const qrCodeUrl = ref('')
const meetingName = ref('')

// 签到统计数据
const totalExpected = ref(0)
const totalPresent = ref(0)

// 类型定义
interface ScreenStats {
  yingdao: number
  shidao: number
}

interface ApiResponse<T> {
  code: number
  message: string
  result: T
}

// 获取签到统计数据
const fetchScreenData = async () => {
  const activeId = route.query.activeId as string
  if (!activeId) return

  try {
    const response = (await Public.getDetailApi({
      url: `/api/screen/detail/${activeId}`,
    })) as unknown as ApiResponse<ScreenStats>

    console.log('签到统计数据:', response)

    if (response.code === 0 && response.result) {
      const stats = response.result
      // 设置应到人数和实到人数
      totalExpected.value = stats.yingdao
      totalPresent.value = stats.shidao
    }
  } catch (error) {
    console.error('获取签到统计数据失败:', error)
    ElMessage.error('获取签到统计数据失败')
  }
}

// 更新时间
const updateTime = () => {
  const now = dayjs()
  currentTime.value = now.format('HH:mm:ss')
}

// 获取当前日期和星期
const updateDateTime = () => {
  const now = dayjs()
  currentDate.value = now.format('YYYY年MM月DD日')
  const weeks = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
  weekDay.value = weeks[now.day()]
}

// 生成二维码
const generateQRCode = async () => {
  const activeId = route.query.activeId as string
  const name = route.query.name as string
  if (activeId) {
    try {
      qrCodeUrl.value = await QRCode.toDataURL(activeId)
      meetingName.value = name || '会议签到'
    } catch (error) {
      console.error('二维码生成失败:', error)
    }
  }
}

let timer: number | null = null

onMounted(() => {
  updateDateTime()
  updateTime()
  generateQRCode()
  fetchScreenData()
  // 每秒更新时间和签到数据
  timer = window.setInterval(() => {
    updateTime()
    fetchScreenData()
  }, 1000)
})

onBeforeUnmount(() => {
  if (timer) {
    window.clearInterval(timer)
  }
})
</script>

<style scoped lang="scss">
.meeting-container {
  min-height: 100vh;
  background: linear-gradient(180deg, #fff1f1 0%, #fff 100%);
  padding: 40px;
  display: flex;
  flex-direction: column;
  background-image: url('https://qushen-outsourcing.oss-cn-beijing.aliyuncs.com/hongyou/signInbac.png');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  position: relative;

  .company-info {
    position: absolute;
    top: 20px;
    left: 20px;
    display: flex;
    align-items: center;
    gap: 10px;
    z-index: 1000;
  }

  .company-logo {
    width: 40px;
    height: 40px;
    object-fit: contain;
  }

  .company-name {
    color: #333;
    font-size: 16px;
    font-weight: 500;
  }

  .meeting-header {
    text-align: center;

    .meeting-title {
      color: #d23e3e;
      font-size: 36px;
      position: relative;
      display: inline-block;
      padding: 0 60px;
      height: 2em;
      background-image: url('https://qushen-outsourcing.oss-cn-beijing.aliyuncs.com/hongyou/signIntit.png');
      background-size: 100% auto;
      background-repeat: no-repeat;
      background-position: bottom;
    }

    .attendance-info {
      display: flex;
      justify-content: center;
      gap: 40px;
      margin-top: 20px;

      .info-item {
        display: flex;
        align-items: center;
        gap: 10px;
        background: rgba(210, 62, 62, 0.1);
        padding: 8px 20px;
        border-radius: 20px;

        .label {
          color: #666;
          font-size: 16px;
        }

        .value {
          color: #d23e3e;
          font-size: 24px;
          font-weight: 600;
          min-width: 40px;
        }
      }
    }
  }

  .meeting-content {
    flex: 1;
    display: flex;
    align-items: center;
    padding: 0 60px;

    .time-container {
      width: 100%;
      text-align: center;

      .digital-time {
        font-size: 72px;
        font-weight: bold;
        margin-bottom: 20px;
        font-family: 'Arial', sans-serif;
      }

      .date-info {
        font-size: 28px;
        margin-bottom: 50px;

        .week {
          margin-left: 15px;
        }
      }

      .qr-section {
        .qr-tip {
          font-size: 20px;
          margin: 20px 0;
        }

        .qr-desc {
          color: #666;
          line-height: 1.8;
          font-size: 14px;
        }
      }
    }
  }
}

.h-full {
  height: 100%;
}

.flex {
  display: flex;
  justify-content: center;
}

.items-center {
  align-items: center;
}
</style>
